import {
    ShopAdjoin
} from "./adjoin.js"

Page({
    data: {
        spu: null
    },

    onLoad() {
        this.init()
    },

    init() {
        const spu = wx.getStorageSync("spu")
        this.setData({
            spu
        })
        console.time("init-time")
        const product = spu

        const data = product.sku_list

        // 商品默认sku
        const pIndex = product.default_sku_id
        const productSku = data[pIndex]
        const specsS = Array.from({
            length: productSku.specs.length
        })
        // 初始化数组
        const commoditySpecs = Array.from({
            length: productSku.specs.length
        })
        // console.log(commoditySpecs, 333)
        const selected = []
        // 提取规格属性列表
        const arr = Array.from({
            length: spu.sku_list[0].specs.length
        })
        spu.sku_list.forEach(item => {
            item.specs.forEach((s, index) => {
                let list = {
                    value_id: s.value_id,
                    value: s.value,
                    selected: false,
                    disabled: false
                }
                if (!arr[index]) {
                    arr[index] = {
                        keyId: s.key_id,
                        key: s.key,
                        list: [list]
                    }
                }
                if (this.container(arr[index].list, list) === -1) arr[index].list.push(list)
            })
        })

        const shopAdjoin = new ShopAdjoin(arr, data)
        this.setData({
            arr,
            product,
            shopAdjoin,
            productSku,
            selected,
            specsS,
            pIndex
        })
        console.timeEnd("init-time")
    },
    // 判断一个数组中是否某个元素
    container(vert, id) {
        let tem = -1
        vert.forEach((item, index) => {
            if (item.value_id === id.value_id) {
                tem = index
            }
        })
        return tem
    },

    selectTap(e) {
        console.time("onclike")
        const cell = e.detail.cell
        const row = e.detail.name
        const arr = this.data.arr

        let specsS = this.data.specsS

        let selected = []

        // 禁用不可选
        if (cell.disabled) return

        // 已选
        if (!specsS[row]) {
            specsS[row] = cell
        } else {
            specsS[row] = specsS[row].value_id == cell.value_id ? null : cell
        }

        arr[row].list.map(({
            value_id,
            value,
            selected,
            disabled
        }, index) => {
            if (value_id == cell.value_id) {
                arr[row].list[index].selected = !selected
            } else {
                arr[row].list[index].selected = false
            }
        })


        /**
         * 获取所有可选项目
         */
        const allCanChoose = this.data.shopAdjoin.querySpecsOptions(specsS)
        arr.map(({
            key_id,
            key,
            list
        }, index) => {
            // 如果没选中，则加进提示数组
            if (!specsS[index]) selected.push(key)

            list.map(({
                value_id,
                value,
                select,
                disable
            }, i) => {
                // 将不在可选项表中的选项禁用
                arr[i].disable = this.container(allCanChoose, list[i]) > -1
            })
        })

        this.setData({
            arr
        })
        console.timeEnd("onclike")
    }

})